import {regExp} from "./regExp.js"
export const rules = {
    isNull(required,msg) {
        return[
            {
                required,
                message:msg+'不能为空'
            }
        ]
    },
    minMaxLength(required,max,msg){
        return [
            {
                required,
                message:msg+'不能为空'
            },
            {
                max,
                message:`${msg}不能超过${max}个字符`
            },
        ]  
    },
    phone(required,msg) {
        return [
            {
                required,
                validator: async (rule, value) => {
                    if (required == true && !value) return Promise.reject('请输入手机号码');
                    if(!value) return;
                    if (!regExp['tel'].test(value)) return Promise.reject(msg?msg:'请输入正确的手机号码');
                    return Promise.resolve();
                },
                trigger: 'change',
            }
        ]
    },
    email(required) {
        return [
            {
                required,
                validator: async (rule, value) => {
                    if (required == true && !value) return Promise.reject('请输入邮箱');
                    if(!value) return;
                    if (!regExp['email'].test(value)) return Promise.reject('请输入正确的邮箱地址');
                    return Promise.resolve();
                },
                trigger: 'change',
            }
        ]

    },
    special(required, message) {
        return [
            {
                required,
                validator: async (rule, value) => {
                    if (required == true && !value) return Promise.reject(`请输入${message}`);
                    if (!/^[0-9\u4e00-\u9fa5a-z]+$/gi.test(value)) return Promise.reject(`${message}不能包含特殊字符`)
                    return Promise.resolve();
                },
                trigger: 'change',
            }
        ]
    },
    password(required, state) {
        return [
            {
                required,
                validator: async (rule, value) => {
                    if (required == true && !value) return Promise.reject(`请输入密码`);
                    if (state == '1') {
                        if (!regExp['password'].test(value)) return Promise.reject(`密码必须为8-20位且包含大写字母、小写字母、数字、特殊字符`);
                    } else {
                        if (value.length < 8 || value.length > 20) return Promise.reject(`密码必须为8-20位`);
                    }
                    return Promise.resolve();
                }
            }
        ]
    },
    mobile(required,msg){
        return [
            {
                required,
                validator: async (rule, value) => {
                    console.log(regExp['mobile'].test(value))
                    if (required == true && !value) return Promise.reject('请输入电话号码');
                    if(!value) return;
                    if (!regExp['mobile'].test(value)) return Promise.reject(msg?msg:'请输入正确电话号码');
                    return Promise.resolve();
                },
                trigger: 'change',
            }
        ]
    }
}

//判断为正整数
export const limitNumber = (value)=> {
    if (typeof value === 'string') {
      return !isNaN(Number(value)) ? value.replace(/\./g, '') : 0
    } else if (typeof value === 'number') {
      return !isNaN(value) ? String(value).replace(/\./g, '') : 0
    } else {
      return 0
    }
}

//-------------------------------------表单校验--------------------------------------

//判断为正整数
export const limitNumber2 = (_rule, value) => {
    value = value+""
    if (!value) {
      return Promise.reject(`${_rule.title}不可为空`);
    }
    const r = /^[1-9][0-9]*$/
    if(r.test(value)){
      return Promise.resolve();
    }else{
      return Promise.reject('只能输入正整数');
    }
}

//判断为数字 或字母 且长度长度为rule.len
export const isNumberLetter = (rule,value)=>{
    if(value == null){
        return Promise.reject(rule.message)
    }
    if(rule.len && value.length != rule.len){
        return Promise.reject(rule.message)
    }
    let copyValue = value.replace(/[\W]/g,'')
    if(copyValue != value){
        return Promise.reject(rule.message)
    }
    return Promise.resolve();
}

// 判断正整数和正小数
export const isNumber = (_rule,value)=>{
    if (!value) {
        return Promise.reject(`${_rule.title}不可为空`);
    }
    const reg = /^([1-9][0-9]*|0)(\.[0-9]*[0-9])?$/
    if (!reg.test(value) || value*1 == 0) {
        return Promise.reject("只能输入正整数或正小数");
    } else {
        return Promise.resolve();
    }
}



